package com.sqly.doc;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class ImageLook extends Activity {

	static ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.img_look);
		byte[] bis = getIntent().getByteArrayExtra("bitmap");
		Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
		imageView = (ImageView) findViewById(R.id.img);
		WindowManager wm = (WindowManager) getApplicationContext().getSystemService(
				Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		bmp=zoomImg(bmp, 1, 1);
		imageView.setImageBitmap(bmp);
		imageView.setOnTouchListener(new TouchListener());
	}

	public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
		int width = bm.getWidth();
		int height = bm.getHeight();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		Matrix matrix = new Matrix();
		float w=0.4f,h=0.4f;
		matrix.postScale(w, h);
		Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				true);
		return newbm;
	}

	private final class TouchListener implements OnTouchListener {

		private int mode = 0;// ��ʼ״̬
		private static final int MODE_DRAG = 1;
		private static final int MODE_ZOOM = 2;

		private PointF startPoint = new PointF();
		private Matrix matrix = new Matrix();
		private Matrix currentMatrix = new Matrix();

		private float startDis;
		private PointF midPoint;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				mode = MODE_DRAG;
				currentMatrix.set(imageView.getImageMatrix());
				startPoint.set(event.getX(), event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				if (mode == MODE_DRAG) {
					float dx = event.getX() - startPoint.x; // �õ�x����ƶ�����
					float dy = event.getY() - startPoint.y; // �õ�x����ƶ�����
					matrix.set(currentMatrix);
					matrix.postTranslate(dx, dy);
				}
				else if (mode == MODE_ZOOM) {
					float endDis = distance(event);// �������
					if (endDis > 10f) { // ������ָ��£��һ���ʱ�����ش���10
						float scale = endDis / startDis;// �õ����ű���
						matrix.set(currentMatrix);
						matrix.postScale(scale, scale, midPoint.x, midPoint.y);
					}
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_POINTER_UP:
				mode = 0;
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				mode = MODE_ZOOM;
				startDis = distance(event);
				if (startDis > 10f) { // ������ָ��£��һ���ʱ�����ش���10
					midPoint = mid(event);
					currentMatrix.set(imageView.getImageMatrix());
				}
				break;
			}
			imageView.setImageMatrix(matrix);
			return true;
		}

		/** ����������ָ��ľ��� */
		private float distance(MotionEvent event) {
			float dx = event.getX(1) - event.getX(0);
			float dy = event.getY(1) - event.getY(0);
			return  (float)Math.sqrt(dx * dx + dy * dy);
		}

		private PointF mid(MotionEvent event) {
			float midX = (event.getX(1) + event.getX(0)) / 2;
			float midY = (event.getY(1) + event.getY(0)) / 2;
			return new PointF(midX, midY);
		}

	}

}
